define(["angularAMD"], function (angularAMD) {
    angularAMD.service('QT02_filterConditionService', ['$state','$rootScope', '$filter', '$stateParams', 'config', '$mdDialog', 'MMWService',
        function ($state, $rootScope, $filter, $stateParams, config, $mdDialog, $MMWService) {

            this.init = function(callbackBomData){
                openDialog(callbackBomData)
            };

            function openDialog(callbackBomData){
                $mdDialog.dialog(
                    'module/moduleQT/program/QT02/QT02_filterCondition.tmp.html',
                    function (dialog) {
                        return {
                            css_url:'module/moduleQT/program/QT02/QT02_filterCondition.css',
                            //分頁設定
                            List_PageConfig: {
                                page: 0,
                                page_count: 25
                            }, 
                            List_filterValue: {},
                            back:function(){
                                dialog.hide();
                            },
                            search:function(){
                                // 如果沒有輸入條件，會帶出所有資料。
                                var content = {
                                    conditon:{
                                        conditon_item_code: dialog.List_filterValue.product_no,
                                        conditon_item_name: dialog.List_filterValue.product_name,
                                        conditon_item_spec: dialog.List_filterValue.product_spec,
                                        conditon_lot_code: dialog.List_filterValue.batch_no
                                    },
                                    base_data:{},
                                    subset_query_conditon: []
                                };
                                searchData(content,function(data){
                                    if(data && data.filter_condition_details.length > 0){
                                        dialog.filter_condition_details = data.filter_condition_details;
                                    } else {
                                        $rootScope.showAlert($filter('translate')('common.msg.no_data'));
                                    };                   
                                });
                            },
                            itemClick: function(item){
                                dialog.selectItem = item;
                            },
                            confirm: function(){
                                var content = {
                                    base_data:{
                                        item_code: dialog.selectItem.product_no,
                                        item_name: dialog.selectItem.product_name,
                                        item_spec: dialog.selectItem.product_spec,
                                        lot_code: dialog.selectItem.batch_no
                                    },
                                    subset_query_conditon: []
                                };
                                bomDataDetails(content,function(data){
                                    if(data && data.bom_level_details.length > 0){
                                        dialog.hide();
                                        callbackBomData(data.bom_level_details);
                                    } else {
                                        $rootScope.showAlert($filter('translate')('common.msg.no_data'));
                                    }; 
                                })
                            },
                            //分頁
                            getPages: function (list, PageConfig, filterItem) {
                                //分頁
                                if (list) {
                                    var temp_list = list.filter(function (item, index, array) {
                                        var isOK = true,
                                            keys = Object.keys(filterItem);
                                        for (var i = 0; i < keys.length; i++) {
                                            var key = keys[i];
                                            if (isOK && item[key] != null && filterItem[key] && filterItem[key] != '') {
                                                if (item[key].indexOf(filterItem[key]) == -1) {
                                                    isOK = false;
                                                    break;
                                                }
                                            }
                                        }
                                        return isOK;
                                    });
                                    if (PageConfig) {
                                        if (temp_list.length != 0) {
                                            PageConfig.total_page = Math.ceil(temp_list.length / PageConfig.page_count);

                                            if (PageConfig.page >= PageConfig.total_page) {
                                                PageConfig.page = 0;
                                            }
                                            var index = PageConfig.page * PageConfig.page_count;
                                            return temp_list.slice(index, index + PageConfig.page_count);
                                        } else {
                                            PageConfig.total_page = 1;
                                            PageConfig.page = 0;
                                        }
                                    } else {
                                        return temp_list;
                                    }
                                } else {
                                    return list;
                                }
                            }
                        };
                    }
                );
            };

            // 查詢帶出符合的資料
            function searchData(content,callback){
                // var data = {
                //     filter_condition_details : [
                //         {
                //             product_no:'JC2001',
                //             product_name:'ABS塑膠粒8502',
                //             product_spec:'綠色',
                //             batch_no:'P22017061901',
                //         },
                //         {
                //             product_no:'JC2001',
                //             product_name:'ABS塑膠粒8502-1',
                //             product_spec:'綠色',
                //             batch_no:'P22017061901-1',
                //         },
                //         {
                //             product_no:'JC2001',
                //             product_name:'ABS塑膠粒8502-2',
                //             product_spec:'綠色',
                //             batch_no:'P22017061901-2',
                //         },
                //     ]
                // }
                // if(callback)
                //     callback(data);

                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_LEAN.product_tracing_data_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
            };

            // 帶出 BOM階資料
            function bomDataDetails(content, callback){
                // var data = {
                //     bom_level_details: [
                //         {lev:"0", parent_item:"JC2001/ABS塑膠粒8502",praent_lot:"綠色P22017061901"},
                //         {lev:"1", parent_item:"JC2001/ABS塑膠粒8502",praent_lot:"綠色P22017061901",sub_item:"JC2001/ABS塑膠粒8502-1",sub_lot:"綠色P22017061901-1"},
                //         {lev:"1", parent_item:"JC2001/ABS塑膠粒8502",praent_lot:"綠色P22017061901",sub_item:"JC2001/ABS塑膠粒8502-1",sub_lot:"綠色P22017061901-1"},
                //         {lev:"2", parent_item:"JC2001/ABS塑膠粒8502-1",praent_lot:"綠色P22017061901-1",sub_item:"JC2001/ABS塑膠粒8502-2",sub_lot:"綠色P22017061901-2"},
                //         {lev:"2", parent_item:"JC2001/ABS塑膠粒8502-1",praent_lot:"綠色P22017061901-1",sub_item:"JC2001/ABS塑膠粒8502-2",sub_lot:"綠色P22017061901-2"},
                //         {lev:"3", parent_item:"JC2001/ABS塑膠粒8502-2",praent_lot:"JC2001/ABS塑膠粒8502-2",sub_item:"JC2001/ABS塑膠粒8502-3",sub_lot:"綠色P22017061901-3"},
                //         {lev:"3", parent_item:"JC2001/ABS塑膠粒8502-2",praent_lot:"JC2001/ABS塑膠粒8502-2",sub_item:"JC2001/ABS塑膠粒8502-3",sub_lot:"綠色P22017061901-3"},
                //         {lev:"4", parent_item:"JC2001/ABS塑膠粒8502-3",praent_lot:"JC2001/ABS塑膠粒8502-3",sub_item:"JC2001/ABS塑膠粒8502-4",sub_lot:"綠色P22017061901-4"},
                //         {lev:"4", parent_item:"JC2001/ABS塑膠粒8502-3",praent_lot:"JC2001/ABS塑膠粒8502-3",sub_item:"JC2001/ABS塑膠粒8502-4",sub_lot:"綠色P22017061901-4"},
                //         {lev:"5", parent_item:"JC2001/ABS塑膠粒8502-4",praent_lot:"JC2001/ABS塑膠粒8502-4",sub_item:"JC2001/ABS塑膠粒8502-5",sub_lot:"綠色P22017061901-5"},
                //         {lev:"5", parent_item:"JC2001/ABS塑膠粒8502-4",praent_lot:"JC2001/ABS塑膠粒8502-4",sub_item:"JC2001/ABS塑膠粒8502-5",sub_lot:"綠色P22017061901-5"},
                //         {lev:"6", parent_item:"JC2001/ABS塑膠粒8502-5",praent_lot:"JC2001/ABS塑膠粒8502-5",sub_item:"JC2001/ABS塑膠粒8502-6",sub_lot:"綠色P22017061901-6"},
                //         {lev:"6", parent_item:"JC2001/ABS塑膠粒8502-5",praent_lot:"JC2001/ABS塑膠粒8502-5",sub_item:"JC2001/ABS塑膠粒8502-6",sub_lot:"綠色P22017061901-6"},
                //         {lev:"7", parent_item:"JC2001/ABS塑膠粒8502-6",praent_lot:"JC2001/ABS塑膠粒8502-6",sub_item:"JC2001/ABS塑膠粒8502-7",sub_lot:"綠色P22017061901-7"},
                //         {lev:"7", parent_item:"JC2001/ABS塑膠粒8502-6",praent_lot:"JC2001/ABS塑膠粒8502-6",sub_item:"JC2001/ABS塑膠粒8502-7",sub_lot:"綠色P22017061901-7"},
                //         {lev:"8", parent_item:"JC2001/ABS塑膠粒8502-7",praent_lot:"JC2001/ABS塑膠粒8502-7",sub_item:"JC2001/ABS塑膠粒8502-8",sub_lot:"綠色P22017061901-8"},
                //         {lev:"8", parent_item:"JC2001/ABS塑膠粒8502-7",praent_lot:"JC2001/ABS塑膠粒8502-7",sub_item:"JC2001/ABS塑膠粒8502-8",sub_lot:"綠色P22017061901-8"},
                //         {lev:"9", parent_item:"JC2001/ABS塑膠粒8502-8",praent_lot:"JC2001/ABS塑膠粒8502-8",sub_item:"JC2001/ABS塑膠粒8502-9",sub_lot:"綠色P22017061901-9"},
                //         {lev:"9", parent_item:"JC2001/ABS塑膠粒8502-8",praent_lot:"JC2001/ABS塑膠粒8502-8",sub_item:"JC2001/ABS塑膠粒8502-9",sub_lot:"綠色P22017061901-9"},
                //         {lev:"10", parent_item:"JC2001/ABS塑膠粒8502-9",praent_lot:"JC2001/ABS塑膠粒8502-9",sub_item:"JC2001/ABS塑膠粒8502-10",sub_lot:"綠色P22017061901-10"},
                //         {lev:"10", parent_item:"JC2001/ABS塑膠粒8502-9",praent_lot:"JC2001/ABS塑膠粒8502-9",sub_item:"JC2001/ABS塑膠粒8502-10",sub_lot:"綠色P22017061901-10"},
                //         {lev:"11", parent_item:"JC2001/ABS塑膠粒8502-10",praent_lot:"JC2001/ABS塑膠粒8502-10",sub_item:"JC2001/ABS塑膠粒8502-11",sub_lot:"綠色P22017061901-11"},
                //         {lev:"11", parent_item:"JC2001/ABS塑膠粒8502-10",praent_lot:"JC2001/ABS塑膠粒8502-10",sub_item:"JC2001/ABS塑膠粒8502-11",sub_lot:"綠色P22017061901-11"},
                //         {lev:"12", parent_item:"JC2001/ABS塑膠粒8502-11",praent_lot:"JC2001/ABS塑膠粒8502-11",sub_item:"JC2001/ABS塑膠粒8502-12",sub_lot:"綠色P22017061901-12"},
                //         {lev:"12", parent_item:"JC2001/ABS塑膠粒8502-11",praent_lot:"JC2001/ABS塑膠粒8502-11",sub_item:"JC2001/ABS塑膠粒8502-12",sub_lot:"綠色P22017061901-12"},
                //         {lev:"13", parent_item:"JC2001/ABS塑膠粒8502-12",praent_lot:"JC2001/ABS塑膠粒8502-12",sub_item:"JC2001/ABS塑膠粒8502-13",sub_lot:"綠色P22017061901-13"},
                //         {lev:"13", parent_item:"JC2001/ABS塑膠粒8502-12",praent_lot:"JC2001/ABS塑膠粒8502-12",sub_item:"JC2001/ABS塑膠粒8502-13",sub_lot:"綠色P22017061901-13"},
                //         {lev:"14", parent_item:"JC2001/ABS塑膠粒8502-13",praent_lot:"JC2001/ABS塑膠粒8502-13",sub_item:"JC2001/ABS塑膠粒8502-14",sub_lot:"綠色P22017061901-14"},
                //         {lev:"14", parent_item:"JC2001/ABS塑膠粒8502-13",praent_lot:"JC2001/ABS塑膠粒8502-13",sub_item:"JC2001/ABS塑膠粒8502-14",sub_lot:"綠色P22017061901-14"},
                //         {lev:"15", parent_item:"JC2001/ABS塑膠粒8502-14",praent_lot:"JC2001/ABS塑膠粒8502-14",sub_item:"JC2001/ABS塑膠粒8502-15",sub_lot:"綠色P22017061901-15"},
                //         {lev:"15", parent_item:"JC2001/ABS塑膠粒8502-14",praent_lot:"JC2001/ABS塑膠粒8502-14",sub_item:"JC2001/ABS塑膠粒8502-15",sub_lot:"綠色P22017061901-15"},
                //         {lev:"16", parent_item:"JC2001/ABS塑膠粒8502-15",praent_lot:"JC2001/ABS塑膠粒8502-15",sub_item:"JC2001/ABS塑膠粒8502-16",sub_lot:"綠色P22017061901-16"},
                //         {lev:"16", parent_item:"JC2001/ABS塑膠粒8502-15",praent_lot:"JC2001/ABS塑膠粒8502-15",sub_item:"JC2001/ABS塑膠粒8502-16",sub_lot:"綠色P22017061901-16"},
                //         {lev:"17", parent_item:"JC2001/ABS塑膠粒8502-16",praent_lot:"JC2001/ABS塑膠粒8502-16",sub_item:"JC2001/ABS塑膠粒8502-17",sub_lot:"綠色P22017061901-17"},
                //         {lev:"17", parent_item:"JC2001/ABS塑膠粒8502-16",praent_lot:"JC2001/ABS塑膠粒8502-16",sub_item:"JC2001/ABS塑膠粒8502-17",sub_lot:"綠色P22017061901-17"},
                //         {lev:"18", parent_item:"JC2001/ABS塑膠粒8502-17",praent_lot:"JC2001/ABS塑膠粒8502-17",sub_item:"JC2001/ABS塑膠粒8502-18",sub_lot:"綠色P22017061901-18"},
                //         {lev:"18", parent_item:"JC2001/ABS塑膠粒8502-17",praent_lot:"JC2001/ABS塑膠粒8502-17",sub_item:"JC2001/ABS塑膠粒8502-18",sub_lot:"綠色P22017061901-18"},
                //         {lev:"19", parent_item:"JC2001/ABS塑膠粒8502-18",praent_lot:"JC2001/ABS塑膠粒8502-18",sub_item:"JC2001/ABS塑膠粒8502-19",sub_lot:"綠色P22017061901-19"},
                //         {lev:"19", parent_item:"JC2001/ABS塑膠粒8502-18",praent_lot:"JC2001/ABS塑膠粒8502-18",sub_item:"JC2001/ABS塑膠粒8502-19",sub_lot:"綠色P22017061901-19"},
                //         {lev:"20", parent_item:"JC2001/ABS塑膠粒8502-19",praent_lot:"JC2001/ABS塑膠粒8502-19",sub_item:"JC2001/ABS塑膠粒8502-20",sub_lot:"綠色P22017061901-20"},
                //         {lev:"20", parent_item:"JC2001/ABS塑膠粒8502-19",praent_lot:"JC2001/ABS塑膠粒8502-19",sub_item:"JC2001/ABS塑膠粒8502-20",sub_lot:"綠色P22017061901-20"},
                //     ]
                // }; 
                // if(callback)
                //     callback(data);

                $MMWService.sendToServer({
                    uri : 'ServicesSTD.Module_LEAN.product_tracing_data_info_get',
                    content : content,
                    success : function(data){
                        if(callback)
                            callback(data);
                    }
                });
                
            };

        }
    ])
});
